package com.fac.controller.admin;

import com.fac.result.Result;
import com.fac.service.StatisticsService;
import com.fac.vo.EquipmentUsageVO;
import com.fac.vo.MaintenanceStatVO;
import com.fac.vo.IncomeStatVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/admin/statistics")
@Api(tags = "管理端-统计分析接口")
@Validated
public class StatisticsController {
    
    @Autowired
    private StatisticsService statisticsService;

    @ApiOperation(value = "设备使用统计", notes = "获取指定时间范围内的设备使用情况统计，包括设备总数、使用中数量、维修中数量、闲置数量、使用率等")
    @ApiResponses({
        @ApiResponse(code = 200, message = "统计成功"),
        @ApiResponse(code = 500, message = "服务器错误")
    })
    @GetMapping("/equipmentUsage")
    public Result<EquipmentUsageVO> getEquipmentUsage(
            @ApiParam(value = "开始日期", example = "2024-01-01", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @ApiParam(value = "结束日期", example = "2024-01-31", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        return Result.success(statisticsService.getEquipmentUsage(startDate, endDate));
    }

    @ApiOperation(value = "维修统计", notes = "获取指定时间范围内的维修情况统计，包括维修订单总数、完成数、平均维修时间、总维修费用、设备故障排名等")
    @ApiResponses({
        @ApiResponse(code = 200, message = "统计成功"),
        @ApiResponse(code = 500, message = "服务器错误")
    })
    @GetMapping("/maintenance")
    public Result<MaintenanceStatVO> getMaintenanceStats(
            @ApiParam(value = "开始日期", example = "2024-01-01", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @ApiParam(value = "结束日期", example = "2024-01-31", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        return Result.success(statisticsService.getMaintenanceStats(startDate, endDate));
    }

    @ApiOperation(value = "收入统计", notes = "获取指定时间范围内的收入统计，包括总收入、租赁收入、维修收入、每日收入、分类收入等")
    @ApiResponses({
        @ApiResponse(code = 200, message = "统计成功"),
        @ApiResponse(code = 500, message = "服务器错误")
    })
    @GetMapping("/income")
    public Result<IncomeStatVO> getIncomeStats(
            @ApiParam(value = "开始日期", example = "2024-01-01", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @ApiParam(value = "结束日期", example = "2024-01-31", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        return Result.success(statisticsService.getIncomeStats(startDate, endDate));
    }
} 